//using System;
//using System.ComponentModel.DataAnnotations;
//using System.Globalization;
//using System.Linq;
//using UCosmic.Domain;
//using UCosmic.Orm;

//namespace UCosmic.Www.Mvc.Areas.Identity.Models.Roles
//{
//    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
//    public class UniqueRoleNameAttribute : ValidationAttribute
//    {
//        public override string FormatErrorMessage(string name)
//        {
//            return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, UserEnteredRoleName);
//        }

//        private string UserEnteredRoleName { get; set; }

//        public override bool IsValid(object value)
//        {
//            // the value passed as an argument to this method is an instance of our RoleForm viewmodel.
//            // however, it is typed as an object. So, we need to "cast" it to the correct type
//            var model = value as RoleForm;
//            if (model != null)
//            {
//                // store the role name in this object to display it
//                // in the user error message
//                UserEnteredRoleName = model.Name;

//                // open a new database connection
//                using (var context = new UCosmicContext())
//                {
//                    // check if there is another role entity with the same name.
//                    var rolesWithMatchingName = context.Roles.Current().Where(r =>
//                                                                              r.Name.Equals(model.Name, StringComparison.OrdinalIgnoreCase) &&
//                                                                              r.EntityId != model.EntityId);

//                    // when no matching role names are found, name is valid
//                    if (!rolesWithMatchingName.Any())
//                    {
//                        return true;
//                    }
//                } //close the database connection
//            }
//            // if there are no roles with the same name in the db return true, otherwise return false
//            return false;
//        }
//    }
//}